package com.angel.bo.message.event;

import com.angel.bo.message.session.SocketSessionRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

/**
 * @Author: Haibo.Mei [Haibo.Mei@ge.com]
 * @Description: 断开连接监听时间
 * @Date: 11:30 2018/3/1
 */
@Component
@Slf4j
public class LostConnectEventListener {

    @Autowired
    private SocketSessionRegistry webAgentSessionRegistry;

    @Autowired
    private RedisTemplate redisTemplate;


    @EventListener
    public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
        MessageHeaderAccessor accessor = NativeMessageHeaderAccessor.getAccessor(event.getMessage(), SimpMessageHeaderAccessor.class);
        GenericMessage<?> generic = (GenericMessage<?>) accessor.getHeader("simpConnectMessage");
        String sessionId = sha.getSessionId();
        webAgentSessionRegistry.removeSessionId(sessionId);
        log.info(sessionId + "断开连接");

    }
}
